package com.ice.client.web;

import com.ice.client.proto.ProtoMsg;
import com.ice.client.proto.ProtobufClient;
import com.ice.framework.common.annotation.AutoResult;
import com.ice.remoting.data.RemotingCommand;
import com.ice.remoting.exception.RemotingConnectException;
import com.ice.remoting.exception.RemotingSendRequestException;
import com.ice.remoting.exception.RemotingTimeoutException;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

@Slf4j
@AutoResult
@RestController
public class ProtobufController {

    @Autowired
    private ProtobufClient protobufClient;

    @GetMapping("writeProtobuf")
    public void writeProtobuf() throws RemotingSendRequestException, RemotingTimeoutException, InterruptedException {
        RemotingCommand remotingCommand = new RemotingCommand();
        remotingCommand.setOpaque(RemotingCommand.createNewRequestId());
        remotingCommand.setCommand((byte) 1);
        ProtoMsg.Message.Builder builder = ProtoMsg.Message.newBuilder();
        builder.setType(ProtoMsg.HeadType.HERART);
        ProtoMsg.HeartBeatReq.Builder heartBeatReq = ProtoMsg.HeartBeatReq.newBuilder();
        heartBeatReq.setY(2024);
        heartBeatReq.setM(2);
        heartBeatReq.setD(10);
        heartBeatReq.setH(16);
        heartBeatReq.setMi(21);
        heartBeatReq.setS(20);
        builder.setHeartBeatReq(heartBeatReq);
        remotingCommand.setBody(builder.build().toByteArray());
        protobufClient.invokeSync(remotingCommand);
    }

    @GetMapping("handleDevice")
    public void handleDevice() throws RemotingSendRequestException, RemotingTimeoutException, InterruptedException {
        RemotingCommand remotingCommand = new RemotingCommand();
        remotingCommand.setOpaque(RemotingCommand.createNewRequestId());
        ProtoMsg.Message.Builder builder = ProtoMsg.Message.newBuilder();
        ProtoMsg.DeviceInfoReq.Builder deviceInfoReq = ProtoMsg.DeviceInfoReq.newBuilder();
        deviceInfoReq.setDeviceNo("52125658");
        builder.setType(ProtoMsg.HeadType.DEVICE);
        builder.setDeviceInfoReq(deviceInfoReq);
        remotingCommand.setBody(builder.build().toByteArray());
        protobufClient.invokeSync(remotingCommand);
    }

}
